﻿using SmartHotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartHotel.DAL
{
    public class RoomInfoDAL
    {
        public RoomInfoDAL(HotelContext hotelContext)
        {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }

        public int AddRoom(RoomInfo roomInfo) { 
            HotelContext.RoomInfos.Add(roomInfo);
            return HotelContext.SaveChanges();
        }
        /// <summary>
        /// 根据id查房间号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoomInfo GetRoomInfoById(int id) {
            return HotelContext.RoomInfos.Where(x=>x.Id==id).FirstOrDefault();
        }

        public int DeleteRoom(int id) {

            var room = GetRoomInfoById(id);
            if (room != null) { 
                HotelContext.RoomInfos.Remove(room);
            }
            return HotelContext.SaveChanges();  
        }


        public int Update(RoomInfo roomInfo) {
            var room = GetRoomInfoById(roomInfo.Id);
            if (room != null) { 
                room.RoomName = roomInfo.RoomName;
                room.RoomTypeId = roomInfo.RoomTypeId;
                room.BreakFastNum = roomInfo.BreakFastNum;
                room.MaxPeople = roomInfo.MaxPeople;
                room.Price = roomInfo.Price;
                room.Status = roomInfo.Status;  
            }
            return HotelContext.SaveChanges();
        }
        /// <summary>
        /// 根据房型id查找房型对应的所有房间
        /// </summary>
        /// <param name="roomTypeId"></param>
        /// <returns></returns>
        public List<RoomInfo> GetRooms(int roomTypeId) { 
           return HotelContext.RoomInfos.Where(x=>x.RoomTypeId==roomTypeId).ToList();
        }
    }
}
